#include "endwidget.h"
#include <QPainter>
#include "global/myresource.h"
#include "global/globalvar.h"
#include "mytime.h"
#include "commondef.h"
#include "mybtn/mybutton.h"

EndWidget::EndWidget(QWidget *parent) :
    QWidget(parent)
{
    m_continueBtn = new MyButton(this,QSizeF(100.0f,50.0f));
    m_continueBtn->setGeometry(75,480,100,50);
    m_continueBtn->setText(QString(tr("continue")),7);
    connect(m_continueBtn,SIGNAL(myclick()),this,SLOT(continuegame()));

    m_menuBtn = new MyButton(this,QSizeF(100.0f,50.0f));
    m_menuBtn->setGeometry(235,480,100,50);
    m_menuBtn->setText(QString(tr("menu")),7);
    connect(m_menuBtn,SIGNAL(myclick()),this,SLOT(tomenu()));
}

void EndWidget::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    p.drawImage(QRectF(50.0f,80.0f,320.0f,480.0f),
                *MyResource::m_promatbgImage,
                QRectF(0.0f,0.0f,300.0f,243.0f));

    p.drawText(QRectF(80.0f,178.0f,160.0f,40.0f),
               Qt::AlignCenter,
               QString(tr("wrong number")));
    p.drawText(QRectF(240.0f,178.0f,5.0f,40.0f),
               Qt::AlignCenter,
               QString(":"));
    if(globalvar::m_logic.m_ierrorNum)
    {
        if(globalvar::m_logic.m_ierrorNum > 3)
        {
            p.drawText(QRectF(245.0f,178.0f,115.0f,40.0f),
                       Qt::AlignCenter,
                       QString("%1 %2").arg(globalvar::m_logic.m_ierrorNum)
                       .arg(tr("This failure")));
        }
        else
        {
            p.drawText(QRectF(245.0f,178.0f,115.0f,40.0f),
                       Qt::AlignCenter,
                       QString("%1 %2").arg(globalvar::m_logic.m_ierrorNum)
                       .arg(tr("error")));
        }
    }
    else
    {
        p.drawText(QRectF(245.0f,178.0f,80.0f,40.0f),
                   Qt::AlignCenter,
                   QString(tr("perfect")));
    }

    p.drawText(QRectF(80.0f,228.0f,160.0f,40.0f),
               Qt::AlignCenter,
               QString(tr("this time")));
    p.drawText(QRectF(240.0f,228.0f,5.0f,40.0f),
               Qt::AlignCenter,
               QString(":"));
    p.drawText(QRectF(245.0f,228.0f,80.0f,40.0f),
               Qt::AlignCenter,
               globalvar::currentTime().toString());

    p.drawText(QRectF(80.0f,278.0f,160.0f,40.0f),
               Qt::AlignCenter,
               QString(tr("The shortest time")));
    p.drawText(QRectF(240.0f,278.0f,5.0f,40.0f),
               Qt::AlignCenter,
               QString(":"));
    p.drawText(QRectF(245.0f,278.0f,80.0f,40.0f),
               Qt::AlignCenter,
               globalvar::levelTime(globalvar::iCurlevel).toString());

    if(globalvar::m_logic.m_bShowPass)
    {
        p.drawText(QRectF(80.0f,328.0f,160.0f,40.0f),
                   Qt::AlignCenter,
                   QString(tr("this pass")));
        p.drawText(QRectF(240.0f,328.0f,5.0f,40.0f),
                   Qt::AlignCenter,
                   QString(":"));
        if(globalvar::m_logic.m_bpass)
        {
            p.drawText(QRectF(125.0f,348.0f,220.0f,40.0f),
                       Qt::AlignCenter,
                       tr("Congratulations to unlock"));
            p.drawText(QRectF(125.0f,368.0f,220.0f,40.0f),
                       Qt::AlignCenter,
                       passString(globalvar::level()));
        }
        else
        {
            p.drawText(QRectF(145.0f,348.0f,220.0f,40.0f),
                       Qt::AlignCenter,
                       QString("%1%2%3%4%5").arg(globalvar::completeNum())
                       .arg("(+").arg(RemainNum(globalvar::completeNum()))
                       .arg(tr("to unlock")).arg(")"));
        }
    }

}

QString EndWidget::passString(int l)
{
    QString str;
    switch(l)
    {
    case NORMAL_L:
    {
        str = tr("normal level");
    }
        break;
    case MASTER_L:
    {
        str = tr("master level");
    }
        break;
    case GUN_L:
    {
        str = tr("gun level");
    }
        break;
    default:
        break;
    }

    return QString("%1").arg(str);

}

int EndWidget::RemainNum(int n)
{
    int i = 1;
    switch(globalvar::level())
    {
    case NEW_L:
    {
       i = 2 - n;
    }
        break;
    case NORMAL_L:
    {
        i = 3 - n;
    }
        break;
    case MASTER_L:
    {
        i = 4 - n;
    }
        break;
    default:
        break;
    }
    return i;
}

void EndWidget::continuegame()
{
    emit continueGame();
}

void EndWidget::tomenu()
{
    emit Tomenu();
}

